package com.github.cakin.shiro.chapter23.dao;

import com.github.cakin.shiro.chapter23.entity.Authorization;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

/**
 * @className: AuthorizationDaoImpl
 * @description: Authorization 数据访问实现类
 * @date: 2020/6/1
 * @author: cakin
 */
@Repository
public class AuthorizationDaoImpl implements AuthorizationDao {
    /**
     * jdbcTemplate
     */
    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 功能描述：创建一条授权记录
     *
     * @param authorization 授权信息
     * @return Authorization 返回创建的授权信息
     * @author cakin
     * @date 2020/6/1
     */
    public Authorization createAuthorization( final Authorization authorization ) {

        final String sql = "insert into sys_user_app_roles(user_id, app_id, role_ids) values(?,?,?)";

        GeneratedKeyHolder keyHolder = new GeneratedKeyHolder();
        jdbcTemplate.update(new PreparedStatementCreator() {
            public PreparedStatement createPreparedStatement( Connection connection ) throws SQLException {
                PreparedStatement psst = connection.prepareStatement(sql, new String[]{"id"});
                int count = 1;
                psst.setLong(count++, authorization.getUserId());
                psst.setLong(count++, authorization.getAppId());
                psst.setString(count++, authorization.getRoleIdsStr());
                return psst;
            }
        }, keyHolder);
        authorization.setId(keyHolder.getKey().longValue());
        return authorization;
    }

    /**
     * 功能描述：更新一条授权记录
     *
     * @param authorization 授权信息
     * @return Authorization 返回更新的授权信息
     * @author cakin
     * @date 2020/6/1
     */
    public Authorization updateAuthorization( Authorization authorization ) {
        final String sql = "update sys_user_app_roles set user_id=?, app_id=?, role_ids=? where id=?";
        jdbcTemplate.update(
                sql,
                authorization.getUserId(), authorization.getAppId(), authorization.getRoleIdsStr(), authorization.getId());
        return authorization;
    }

    /**
     * 功能描述：删除一条授权记录
     *
     * @param authorizationId 授权记录id
     * @author cakin
     * @date 2020/6/1
     */
    public void deleteAuthorization( Long authorizationId ) {
        final String sql = "delete from sys_user_app_roles where id=?";
        jdbcTemplate.update(sql, authorizationId);
    }


    /**
     * 功能描述：根据id找到对应的授权记录
     *
     * @param authorizationId 授权记录的id
     * @return Authorization 返回更新的授权信息
     * @author cakin
     * @date 2020/6/1
     */
    public Authorization findOne( Long authorizationId ) {
        final String sql = "select id, user_id, app_id, role_ids as roleIdsStr from sys_user_app_roles where id=?";
        List<Authorization> authorizationList =
                jdbcTemplate.query(sql, new BeanPropertyRowMapper<Authorization>(Authorization.class), authorizationId);
        if (authorizationList.size() == 0) {
            return null;
        }
        return authorizationList.get(0);
    }

    /**
     * 功能描述：根据所有的授权记录
     *
     * @return List<Authorization> 授权列表
     * @author cakin
     * @date 2020/6/1
     */
    public List<Authorization> findAll() {
        final String sql = "select id, user_id, app_id, role_ids as roleIdsStr from sys_user_app_roles";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper(Authorization.class));
    }


    /**
     * 功能描述：根据appId和userId找到对应的Authorization
     *
     * @param appId  应用id
     * @param userId 用户id
     * @return Authorization 授权信息
     * @author cakin
     * @date 2020/6/1
     */
    public Authorization findByAppUser( Long appId, Long userId ) {
        final String sql = "select id, user_id, app_id, role_ids as roleIdsStr from sys_user_app_roles where app_id=? and user_id=?";
        List<Authorization> authorizationList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<Authorization>(Authorization.class), appId, userId);
        if (authorizationList.size() == 0) {
            return null;
        }
        return authorizationList.get(0);
    }
}
